package org.skydiveapphd;

import java.text.MessageFormat;

import org.skydiveapphd.ui.FragmentHelper;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.AltitudeUnit;
import org.skydiveapplibrary.data.Statistics;
import org.skydiveapplibrary.repository.StatisticsRepository;
import org.skydiveapplibrary.ui.FormatUtility;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableRow;
import android.widget.TextView;

public class SummaryActivity extends Activity
{
	private StatisticsRepository repository;
	
	// listeners
	private OnClickListener summaryExtendedListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{		
			FragmentHelper.getInstance().openFragment(new SummaryExtendedActivity(), android.R.id.tabhost, 
					Constants.TAG_SUMMARY_EXTENDED, getParent().getFragmentManager());
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
    	
		repository = new StatisticsRepository(this);
		
		// show view
		setContentView(R.layout.summary_view);
		
		// set header label
		setTextValue(R.id.EditHeaderTextView, getString(R.string.main_summary_tab_label));
		
		//Listeners
    	TableRow jumpCountRow = (TableRow)findViewById(R.id.StatisticsJumpCountRow);
    	jumpCountRow.setOnClickListener(summaryExtendedListener);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		loadData();
	}
	
	public void loadData()
	{
		// get stats
		Statistics stats = repository.getStatistics();
		
		// set fields
		setTextValue(R.id.StatisticsTotalJumpCountField, stats.getTotalJumpCount());
		setTextValue(R.id.StatisticsTotalCutawayCountField, stats.getTotalCutawayCount());
		setTextValue(R.id.StatisticsTotalFreefallTimeField, getFreefallTimeString(stats.getTotalFreefallTime()));
		setTextValue(R.id.StatisticsTotalFreefallDistanceField, getFreefallDistanceString(
				stats.getTotalFreefallDistance(),
				stats.getAltitudeUnit()));
		setTextValue(R.id.StatisticsMaxExitAltField, getAltitudeString(
				stats.getMaxExitAltitude(),
				stats.getAltitudeUnit()));
		setTextValue(R.id.StatisticsMinDeploymentAltField, getAltitudeString(
				stats.getMinDeploymentAltitude(),
				stats.getAltitudeUnit()));
		setTextValue(R.id.StatisticsMaxFreefallTimeField, Integer.toString(stats.getMaxFreefallTime()) + " " + getString(R.string.freefall_time_seconds));
		setTextValue(R.id.StatisticsJumpsLastField, FormatUtility.toDateString(getApplicationContext(), stats.getLastJumpDate()));
		setTextValue(R.id.StatisticsJumpsLastYearField, stats.getJumpsInLastYear());
		setTextValue(R.id.StatisticsJumpsLastMonthField, stats.getJumpsInLastMonth());
	}

	private void setTextValue(int id, int val)
	{
		TextView tv = (TextView)findViewById(id);
		tv.setText(FormatUtility.toNumericString(val));
	}
	
	private void setTextValue(int id, String text)
	{
		TextView tv = (TextView)findViewById(id);
		tv.setText(text);
	}
	
	private String getFreefallTimeString(double freeFallTimeSeconds)
	{
		int hours = (int)Math.floor(freeFallTimeSeconds / 3600);
		int secondsLeftFromHours = (int)(freeFallTimeSeconds % 3600);
		int minutes = (int)Math.floor(secondsLeftFromHours / 60);
		int seconds = (int)(secondsLeftFromHours % 60);
		
		if (hours <= 0)
		{
			return MessageFormat.format(
					getString(R.string.freefall_time_ms_format),
					minutes, seconds);
		}
		else
		{
			return MessageFormat.format(
					getString(R.string.freefall_time_hms_format),
					hours, minutes, seconds);
		}
	}
	
	private String getFreefallDistanceString(int totalAltitude, AltitudeUnit unit)
	{
		float totalDistance = AltitudeUnit.convertToDistance(totalAltitude, unit);
		String format = unit == AltitudeUnit.Feet ? getString(R.string.miles_distance_format) :
			getString(R.string.kilometers_distance_format);
		return MessageFormat.format(format, totalDistance);
	}
	
	private String getAltitudeString(int altitude, AltitudeUnit unit)
	{
		return MessageFormat.format(
				getString(R.string.altitude_format),
				altitude,
				getString(unit.getResourceId()));
	}
}
